package odgs.app.server;

import javax.swing.JFrame;

import odgs.Contents;
import odgs.ContentsFile;
import odgs.Directory;
import odgs.ExplorerNode;
import odgs.Result;
import odgs.app.SwingUtil;
import odgs.listener.ServerManagerListener;
import odgs.manager.DBManager;
import odgs.manager.NetworkManager;
import odgs.manager.ServerManager;

public class ServerMainFrameController implements ServerManagerListener
{
	private ServerManagerLogViewController logviewController;
	private ServerMainFrameUI ui;
	private ServerManager serverManager;
	private DBManager dbManager;
	private NetworkManager	networkManager;
	private GridNodeStateTableUIController gridNodeStateTableController;
	private ServerExplorerTableController explorerTableController;
	private GridNodeStateGraphController controller;
	private ServerMenuBar menuBar;

	public ServerMainFrameController(ServerMainFrameUI ui)
	{
		this.ui = ui;
		ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void setServerManager(ServerManager manager)
	{
		serverManager = manager;
		serverManager.addServerManagerListener(this);
	}

	public void error(int error)
	{
		if(error == Result.SERVER_CAN_NOT_OPEN)
		{
			String ment = "Server is already opened!!";
			SwingUtil.showCiritalErrorDialog(ui, ment);
		}
	}

	public void serverOpened(int port)
	{
		initController();
		showMainFrame();
	}		

	private void initController()
	{

		menuBar = new ServerMenuBar(serverManager,ui.exeplrerTable.explorer.explorerTable);
		ui.setJMenuBar(menuBar);
		
		logviewController = new ServerManagerLogViewController(ui.logView);
		logviewController.setServerManager(serverManager);
		
		gridNodeStateTableController = new GridNodeStateTableUIController(ui.gridNdoeTable);
		gridNodeStateTableController.setServerManager(serverManager);
		
		explorerTableController = new ServerExplorerTableController(ui.exeplrerTable);
		explorerTableController.setServerManager(serverManager);

		ui.exeplrerTable.explorer.explorerTable.addMouseListener(new ServerExplorerPopup(ui.exeplrerTable.explorer.explorerTable, serverManager));

		controller = new GridNodeStateGraphController(ui.gridNodeGraph);
		controller.setServerManager(serverManager);
		
		GridStateGraphController controller2 = new GridStateGraphController(ui.gridStateGraph);
		controller2.setServerManager(serverManager);
	}

	private void showMainFrame()
	{
		ui.setResizable(false);
		ui.setVisible(true);
		ui.validate();
	}

	public void setDBManager(DBManager dbManager)
	{
		this.dbManager = dbManager;
	}

	public void setNetworkManager(NetworkManager networkManager)
	{
		this.networkManager = networkManager;
	}

	public void addedDirectory(Directory directory){}
	public void removeDirectory(Directory directory){}
	public void addedContents(Contents contents){}
	public void removeContents(Contents contents){}
	public void useStorage(ContentsFile file){}
	public void freeStorage(ContentsFile file){}
	public void loginedNode(String id){}
	public void joinedNode(String id){}
	public void logout(String id){}
	public void log(String log) {}
	public void updatedExplorerNode(String mode, ExplorerNode node, String name) {}
}